/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ciphertextattack;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author radik
 */
public class utilities { 
    public static Map<String, Boolean> CreateDictionary() throws Exception {
        Map<String, Boolean> new_dictionary = new HashMap<String, Boolean>();
        Stemmer stem = new Stemmer();      
        new_dictionary.put(stem.StemWord("the"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("be"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("to"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("of"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("and"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("a"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("in"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("that"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("have"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("i"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("it"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("for"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("not"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("on"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("with"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("he"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("as"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("you"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("do"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("at"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("this"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("but"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("his"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("by"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("from"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("they"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("we"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("say"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("her"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("she"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("or"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("an"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("will"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("my"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("one"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("all"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("would"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("there"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("their"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("what"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("so"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("up"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("out"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("if"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("about"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("who"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("get"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("which"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("go"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("me"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("when"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("make"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("can"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("like"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("time"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("no"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("just"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("him"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("know"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("take"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("people"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("into"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("year"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("your"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("good"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("some"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("could"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("them"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("see"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("other"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("than"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("then"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("now"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("look"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("only"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("come"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("its"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("over"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("think"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("also"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("back"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("after"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("use"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("two"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("how"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("our"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("work"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("first"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("well"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("way"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("even"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("new"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("want"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("because"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("any"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("these"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("give"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("day"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("most"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("us"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("because"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("good"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("each"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("those"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("feel"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("seem"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("how"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("high"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("too"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("place"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("little"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("world"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("very"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("still"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("nation"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("hand"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("old"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("life"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("tell"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("write"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("become"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("here"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("show"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("house"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("both"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("between"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("need"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("mean"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("call"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("develop"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("under"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("last"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("right"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("move"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("thing"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("general"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("school"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("never"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("same"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("another"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("begin"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("while"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("number"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("part"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("turn"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("real"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("leave"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("might"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("want"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("point"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("form"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("off"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("child"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("few"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("small"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("since"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("against"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("ask"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("later"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("home"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("interest"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("large"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("person"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("end"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("open"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("public"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("follow"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("during"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("present"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("without"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("again"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("hold"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("govern"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("around"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("possible"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("head"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("consider"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("word"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("program"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("problem"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("however"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("lead"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("system"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("set"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("order"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("eye"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("plan"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("run"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("keep"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("face"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("fact"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("group"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("play"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("stand"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("increase"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("early"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("course"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("change"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("help"), Boolean.FALSE);
        new_dictionary.put(stem.StemWord("line"), Boolean.FALSE);  
        return new_dictionary;
    }
    
    public static byte[] readFile(String fileName) throws Exception {
        byte[] bytes = null;
        try {
            bytes = Files.readAllBytes(Paths.get(fileName));
        } 
        catch (IOException e) {
            throw  new Exception("No such file! " + fileName);
        }
        return bytes;
   }
    
    public static List<String> readFileByLine(String fileName) throws Exception {
        List<String> lines = null;
        try {
            lines = Files.readAllLines(Paths.get(fileName));
        } 
        catch (IOException e) {
            throw  new Exception("No such file! " + fileName);
        }
        return lines;
   }
    
    public static void writeToFile(byte[] to_write, String filename) throws Exception {
        
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(filename);
            stream.write(to_write);
        }
        catch(IOException e){
            throw  new Exception("No such file! " + filename);
        }
        finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } 
            catch (IOException e) {
                
                throw  new Exception("Error closing file");
            }
        }
    }
    
    public static void writeToFile(Map<Character, Character> to_write, String filename) throws Exception {
        
        PrintWriter out = null;
        String key_string = "";
        ArrayList<String> list = new ArrayList<String>();
        Iterator it = to_write.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            list.add(pairs.getKey() + " " + pairs.getValue() + "\n");
            it.remove(); // avoids a ConcurrentModificationException
        }
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            key_string+=list.get(i);
        }
        try {
            out = new PrintWriter(filename);
            out.print(key_string.substring(0, key_string.length()-1));
        }
        catch(IOException e){
            throw  new Exception("No such file! " + filename);
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
            } 
            catch (Exception e) {
                
                throw  new Exception("Error closing file");
            }
        }
    }
    
    public static Map<Byte, Byte> createKeyDic(String file_name) throws Exception {
        List<String> key = readFileByLine(file_name);
        Map<Byte, Byte> dic = new HashMap<Byte, Byte>();
        
        int length = key.size();
        for (int i=0;i<length;i++) {
            String line = key.get(i);
            byte[] dic_val = line.getBytes();
            dic.put(dic_val[0], dic_val[2]);
        }
        return dic;
    } 
        
    public static Byte[] createIV(String file_name) throws Exception {
        byte[] bytes = utilities.readFile(file_name);
        Byte[] iv = new Byte[bytes.length];
        for (int i=0;i<bytes.length;i++) {
            iv[i] = bytes[i];
        }
        return iv;
    }
}

